/*
 * Created on 2004-11-16
 *
 * Copyright: Bluejay Networks Inc. & Inquiry Management Systems Ltd.  2004
 *
 * This software is the proprietary information of Bluejay Networks Inc. & 
 * Inquiry Management Systems Ltd.  
 * Use is subject to license terms.
 */
package com.ims.iaa.JavaScript;

import java.rmi.Naming;
import java.rmi.RemoteException;

import com.ims.iaa.IaaDebug;
import com.ims.iaa.IaaUtil;

/**
 * This class provide some useful method to access JavaScript Saver
 *
 * @author ShengMeng Bai
 *
 * @version 1.00, 17 Nov. 2004
 *
 */
public class JSUtil {
	public static final String RMI_NAME = "ScriptSaver";
	//the only single JavaScriptSaver object
	private static JavaScriptSaver m_saver; 
	static{
		m_saver = getJSSaver(IaaDebug.LogWriteToScreen);
	}
	/**
	 * Save the script to a file or screen
	 * 
	 * @param sScript, the Script Content
	 * @param sMessage, save some message befor save script content 
	 * @param sContainer, the script container 
	 */
	public static void SaveJS(String sContent, String sMessage, String sContainer){
		m_saver.saveScript(sContent, sMessage, sContainer);
	}
	public static void SaveJS(String sContent, String sContainer){
		m_saver.saveScript(sContent, sContainer);
	}
	
	private static JavaScriptSaver getJSSaver(boolean saveToScreen){
		if(!saveToScreen){
			//get the saver from RMI
			RemoteJSSaver saver = getRemoteSaver(IaaDebug.ServerHost,IaaDebug.ServerPort);
			if(saver != null){
				return new RemoteSaverAdapter(saver);
			}else{
				IaaUtil.log("Can't get remote script saver, will output to screen.");
			}
		}
		//get the save script to screen
		return new JavaScriptSaver(){
			public void saveScript(String sScript, String sMessage, String sContainer){
				IaaDebug.systemout.println(sMessage + " in " + sContainer);
				IaaDebug.systemout.println(sScript);
			}
			public void saveScript(String sScript, String sContainer){
				IaaDebug.systemout.println("In " + sContainer);
				IaaDebug.systemout.println(sScript);
			}
		};
	}
	
	//get script saver form RMI 
	private static RemoteJSSaver getRemoteSaver(String hostName, int registryPort)  {
		RemoteJSSaver saver = null;
   		String  serverName = "rmi://" + hostName + ":" + registryPort
     			+ "/" + RMI_NAME;
     	try {
			//get the script saver handle
     		saver = (RemoteJSSaver)(Naming.lookup( serverName ));
     	} catch ( Exception e ) {
         	//System.out.println( "Lookup LogWriter failed: " + e );
     	}
		return saver;
	}
}
/**
 * This class adapt RemoteJSSaver to JavaScriptSaver
 */
class RemoteSaverAdapter implements JavaScriptSaver{
	private RemoteJSSaver m_saver;
	RemoteSaverAdapter(RemoteJSSaver saver){
		m_saver = saver;
	}
	public void saveScript(String sScript, String sMessage, String sContainer){
		try{
			m_saver.saveScript(sScript, sMessage, sContainer);
		} catch (RemoteException e)  {
			IaaDebug.systemout.println("Save script Error: " + e);
		}
	}
	public void saveScript(String sScript, String sContainer){
		try{
			m_saver.saveScript(sScript, sContainer);
		} catch (RemoteException e)  {
			IaaDebug.systemout.println("Save script Error: " + e);
		}
	}
}
